<template>
  <view>
    <view class="settle-box">
      <view class="radio">
        <radio color="#c00000" @click="chooseAll" :checked="isFullChecked"></radio>
        <text @click="chooseAll">全选</text>
      </view>
      <view class="amount-box">
        <text>合计:</text>
        <text class="amount">${{totalMoney}}</text>
      </view>
      <view class="btn-settle" @click="settle">
        结算({{totalCheckCount}})
      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'Vuex'
  export default {
    name: "my-settle",
    data() {
      return {
        seconds: 2,
        timeout: 0
      };
    },
    computed: {
      ...mapGetters('m_cart', ['totalMoney', 'totalCheckCount', 'total']),
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_user', ['addressStr']),
      ...mapState('m_user', ['token']),
      isFullChecked() {
        return this.total === this.totalCheckCount
      }
    },
    methods: {
      ...mapMutations('m_cart', ['updateAllGoodsState']),
      ...mapMutations('m_user', ['updateRedirectInfo']),
      chooseAll() {
        this.updateAllGoodsState(!this.isFullChecked)
      },
      async settle() {

        if (this.totalCheckCount === 0) return uni.$showMsg('未选择商品')
        if (!this.addressStr) return uni.$showMsg('未选择收货地址!')
        if (!this.token) return this.delayShow()
        // 支付
        this.payOrder()
      },
      // 展示倒计时
      showTips(n) {
        uni.showToast({
          icon: 'none',
          title: '请登录后再结算!' + n + '秒后自动跳转到登录界面!',
          mask: true,
          duration: 1000
        })
      },
      delayShow() {
        this.showTips(this.seconds)
        this.timeout = setInterval(() => {

          this.seconds--
          if (this.seconds <= 0) {
            clearInterval(this.timeout)
            this.seconds = 2
            uni.switchTab({
              url: '/pages/my/my',
              success: () => {
                this.updateRedirectInfo({
                  type: 'switchTab',
                  from: '/pages/cart/cart'
                })
              }
            })

            return;
          }
          this.showTips(this.seconds)
        }, 1000)
      },
      async payOrder() {
        // 创建订单对象
        const orderInfo = {
          order_price: 0.01,
          consignee_addr: this.addressStr,
          goods: this.cart.filter(x => x.goods_state).map(x => {
            return {
              goods_id: x.goods_id,
              goods_number: x.goods_count,
              goods_price: x.goods_price
            }
          }),
        }
        const {
          data: res
        } = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
        if (res.meta.status !== 200) return uni.$showMsg('订单创建失败!')
        // 订单预支付
        const orderNumber = res.message.order_number
        const payRes = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {order_number:orderNumber})
          // if (res.meta.status !== 200) return uni.$showMsg('订单创建失败!')
          
          
          // 微信支付
          // 调用支付接口
      const x=await uni.requestPayment(payRes)
      // 查询支付状态
        const payStatusRes = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {order_number:orderNumber})
    
        console.log(payRes)
      }
    }
  }
</script>

<style lang="scss">
  .settle-box {
    padding-left: 5px;
    background-color: whitesmoke;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    display: flex;

    justify-content: space-between;
    align-items: center;
    font-size: 14px;

    .radio {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .amount-box {
      .amount {
        color: #c00000;
      }
    }

    .btn-settle {
      height: 50px;
      min-width: 100px;
      color: white;
      background-color: #c00000;
      text-align: center;
      line-height: 50px;
    }
  }
</style>